package com.hcmus.armobile;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import android.R.id;
import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class SendMessageActivity extends Activity {
	
	// UI elements
		EditText txtMessage;
		EditText txtLat;
		EditText txtLong;
		EditText txtDis;
		TextView result;
		
		// Register button
		Button btnSend;
		
		// Asyntask
				AsyncTask<Void, Void, Void> mRegisterTask;
				
				
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//setContentView(R.layout.sendmessage);
		
/*
		txtMessage = (EditText) findViewById(R.id.txtmsSend);
		txtLat = (EditText) findViewById(R.id.txtLat);
		txtLong = (EditText) findViewById(R.id.txtLong);
		txtDis =(EditText)findViewById(R.id.txtDis);
		//result = (TextView) findViewById(R.id.txtResult);
		btnSend = (Button) findViewById(R.id.btnSend);*/
		try{
		btnSend.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// Read EditText dat
				String mesage = txtMessage.getText().toString();
				String lat = txtLat.getText().toString();
				String log = txtLong.getText().toString();
				String dis = txtDis.getText().toString();
				final Map<String, String> params = new HashMap<String, String>();
		        params.put("message", mesage);
		        params.put("lat", lat);
		        params.put("long", log);
		        params.put("dis", dis);
		        
		        final String serverUrl = "http://192.168.1.15/gcm_server_php/testpost.php";
		        
		        mRegisterTask = new AsyncTask<Void, Void, Void>() {

					@Override
					protected Void doInBackground(Void... paramss) {
						//ServerUtilities.post(serverUrl,params);
						 DoPost(serverUrl,params);
						 
						return null;
					}
					
					private void DoPost(String serverUrl, Map<String, String> params) {
						// TODO Auto-generated method stub
						try {
							String kq = ServerUtilities.postRecive(serverUrl,params);
							System.out.println(kq);
							//Toast.makeText(this, kq,);
							//Toast.makeText(getApplicationContext(), kq, Toast.LENGTH_LONG).show();
							
							//AlertDialogManager alert = new AlertDialogManager();
							//alert.showAlertDialog(SendMessageActivity.this, "Result: ", kq, true);
							
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						} 
						
						
					}

					@Override
					protected void onPostExecute(Void result) {
						mRegisterTask = null;
					}

				};
				mRegisterTask.execute(null, null, null);
				
		        
		       
			}
		});
		}catch(Exception ex){
			//Log.i("error" +ex.toString() );
		}

		/*btnSend.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
				String mesage = txtMessage.getText().toString();
				String lat = txtLat.getText().toString();
				String log = txtLong.getText().toString();
				
				Map<String, String> params = new HashMap<String, String>();
		        params.put("message", mesage);
		        params.put("lat", lat);
		        params.put("long", log);
		        
		        String serverUrl = "http://192.168.1.15/gcm_server_php/testpost.php";
		        try {
					ServerUtilities.post(serverUrl,params);
				} catch (IOException e) { 
					// TODO Auto-generated catch block
					Log.v(TAG, "error when posting server:"+ serverUrl +" : "+e.toString() );
					
					//e.printStackTrace();
				}
		        
		        
				
			}
		});*/
	
	}
	

}
